import Ability from '@ohos.application.Ability'

export default class MainAbility extends Ability {
    onCreate(want, launchParam) {
        console.log("[Demo] MainAbility onCreate")
        let abilityInfo = this.context.abilityInfo;
        globalThis.bundleName = abilityInfo.bundleName
        globalThis.abilityWant = want;
        globalThis.context = this.context
        globalThis.LogTag = 'cwq'
        globalThis.isShowLog = true
        globalThis.display
        globalThis.abilityContext= this.context

    }

    onDestroy() {
        console.log("[Demo] MainAbility onDestroy")
    }

    onWindowStageCreate(windowStage) {
        // Main window is created, set main page for this ability
        console.log("[Demo] MainAbility onWindowStageCreate")
        globalThis.windowStage = windowStage
        //        globalThis.context = this.context
        this.requestPermissions(windowStage)
//        windowStage.setUIContent(this.context, "pages/ClassifyIndex", null)

    }

    onWindowStageDestroy() {
        // Main window is destroyed, release UI related resources
        console.log("[Demo] MainAbility onWindowStageDestroy")
    }

    onForeground() {
        // Ability has brought to foreground
        console.log("[Demo] MainAbility onForeground")
    }

    onBackground() {
        // Ability has back to background
        console.log("[Demo] MainAbility onBackground")
    }

    private requestPermissions(windowStage) {

        let permissionList: Array<string> = [
            "ohos.permission.READ_MEDIA",
            "ohos.permission.WRITE_MEDIA",
            "ohos.permission.MEDIA_LOCATION",
            "ohos.permission.STORAGE_MANAGER",
            "ohos.permission.INSTALL_BUNDLE"
        ]
        this.context.requestPermissionsFromUser(permissionList).then(function (data) {
            console.log('request permission data result = ' + data.authResults)
            globalThis.windowStage.setUIContent(globalThis.context, "pages/ClassifyIndex", null)
        }, (error) => {
            console.log( 'fail to request permission error code = ' + error.code)
        })
    }
};
